const express = require('express')
const getAccessToken = require('./tools/getAccessToken')
const sendMsg = require('./tools/sendMsg')
const app = express()

//连接数据库
require('./mongoose')

//验证服务器中间件
app.use(require('./middleware/verifyServerRouter'))

//消息预处理中间件
app.use(require('./middleware/paseMsgMW'))

//消息服务中间件
app.use(require('./middleware/messageRouter'))

//测试代码
// app.post('/wx', async(req, res)=>{
//     const{ jsonData} = req 
//     console.log('ok')
//     const sedMsg = {
//         to: jsonData.ToUserName,
//         picID: 'H3-bcY1sCXMll3wosp61tV0kGgdXQB8eigq0Bm4Z7WO9THJeaodfFrngJD6rL0mh',
//         address: '32#802'
//     }
//     const axios = require('axios')
//     const result = await axios.post('http://localhost:3002/wx', {...sendMsg})

//     const xmlSendData = require('./tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], jsonData.Content[0].split('').reverse().join(''))
              
//     res.end(xmlSendData)
// })

//获取token
// app.get('/token', async (req, res) => {
//     const token = await getAccessToken()
//     res.json(token)
// })

// //解析地址
// app.get('/address', async (req, res) => {
//     //console.log(req.query)
//     if (req.query.address) {
//         const address = await require('./baiduAPI/baiduAddressVerify')(req.query.address)
//         //console.log(address)
//         res.json(address)
//     } else {
//         res.end('error!')
//     }
// })


// app.get('/addsevice', async (req, res) => {
//     const axios = require('axios')
//     const token = await getAccessToken()
//     const kf = {
//         "kf_account": "test1@test",
//         "nickname": "客服1",
//         "password": "pswmd5"
//     }
//     console.log(token)
//     const { data } = await axios.post(`https://api.weixin.qq.com/customservice/kfaccount/add?access_token=${token.access_token}`, kf)
//     console.log(data)
// })

//处理未知情况返回404
app.use(require('./errorHandler'))

app.listen('3000', () => {
    console.log('server started at port 3000')
})